<?php
class Catalog_SpecialController extends Areal_Controller_Action
{
    public function indexAction()
    {
        $special = new Catalog_Model_Special();
        $this->view->products = $special->getCurrentSpecialProductList();
    }

    public function adminAction()
    {
        $sort = $this->getRequest()->getParam('sort');
        $filter = new Catalog_Model_SpecialAdminFilter();
        $current_page = $this->_getParam('page');
        if ($sort){
            $filter->setSort($sort);
            return $this->_helper->redirector('admin','special','catalog',
                                                  array('page'=>$current_page));
        }
        if ($this->getRequest()->isPost()){
            $filter->setData($this->getRequest()->getPost());
        }
        $this->view->assign('specials', $filter->getAdminPaginator($current_page));
        $this->view->assign('filterForm', $filter->getForm() );
    }

    public function addAction()
    {
        $product_id = $this->getRequest()->getParam('product_id');
        $special = new Catalog_Model_Special();
        $special->product_id = $product_id;
        if ($this->getRequest()->isPost()){
            if ($special->edit($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Special saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','category','catalog',
                                                  array('category_id'=>$special->Product->category_id));
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Special not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        } else {
            $special->getEditingForm()->price->setValue( $special->Product->price );
        }
        $this->view->special = $special;
        Catalog_Model_Category::addCategoriesToNavigation($this->view->navigation(), $special->Product->category_id);
    }

    public function editAction()
    {
        $special_id = $this->getRequest()->getParam('special_id');
        $special = Doctrine::getTable('Catalog_Model_Special')->find($special_id);
        if ($this->getRequest()->isPost()){
            if ($special->edit($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Special saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','special','catalog');
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Special not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->special = $special;
    }

    public function deleteAction()
    {
        $special_id = $this->getRequest()->getParam('special_id');
        $special = Doctrine::getTable('Catalog_Model_Special')->find($special_id);
        $form = new Default_Form_ConfirmForm();
        if ($this->getRequest()->isPost()){
            if ($form->isValid($this->getRequest()->getPost())){
                $special->delete();
                $message = Zend_Registry::get('Zend_Translate')->translate('Special deleted');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','special','catalog');
            }
        }
        $this->view->form = $form;
    }
}